package com.weiyuwqj.controller.account;

import com.weiyuwqj.api.constant.Message;
//import com.weiyuwqj.api.constant.SystemEnum;
import com.weiyuwqj.api.service.BusAccountRechargeService;
import com.weiyuwqj.api.service.BasUserService;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.util.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * Created by zhangyong on 2016/11/24.
 * 充值及充值记录
 */
@Controller
@RequestMapping("/account/recharge")
public class AccountRechargeController {

    @Autowired
    BusAccountRechargeService accountRechargeService;
    @Autowired
    BasUserService userService;

    //充值纪录页面
    @RequestMapping("/rechargeRecord")
    public Object rechargeRecord() {
        return "main/user/rechargeRecord";
    }

    /**
     * 充值记录
     * @param request
     * @param p
     * @return
     */
    @ResponseBody
    @RequestMapping("/rechargeRecordList")
    public Object rechargeRecordList(HttpServletRequest request,Paginator p){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        return accountRechargeService.getListByUserId(p, userId);
    }

    //充值页面
    @RequestMapping("/recharge")
    public Object recharge(){
        return "/main/user/recharge";
    }

    /**
     * web充值
     * @param request
     * @param rechargeAmount 充值金额
     * @param pictureCode 图片验证码
     * @return
     */
    @RequestMapping(value = "/platformRecharge")
    public Object platformRecharge(HttpServletRequest request, BigDecimal rechargeAmount,String pictureCode) {

        Integer userId = (Integer) request.getSession().getAttribute("userId");
        String verifyCode = request.getSession().getAttribute("verifyCode").toString();

        //校验图片验证码
//        if(!verifyCode.equals(pictureCode)) {
//            throw new CrowdException("picture_code_error");
//        }
        if (rechargeAmount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new CrowdException("platformRecharge.amountEqualToZero");
        }
        if (rechargeAmount.compareTo(BigDecimal.ZERO) == 0) {
            throw new CrowdException("platformRecharge.amountEqualToZero");
        }
   /*     Map payInfo=accountRechargeService.platformRecharge(userId,rechargeAmount.setScale(2,BigDecimal.ROUND_DOWN),"C001","网银充值");
        request.setAttribute("payInfo",payInfo);
*/
        return "/main/pay";
    }

    /**
     * 查询银行当日充值限额
     */
    @ResponseBody
    @RequestMapping(value="/rechargeLimit")
    public Object rechargeLimit(HttpServletRequest request) {
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        return null;//accountRechargeService.getRechargeLimit(userId);
    }
    /**
     * 图片验证码校验
     * @param request
     * @param pictureCode
     * @return
     */
    @ResponseBody
    @RequestMapping("/pictureCodeCheck")
    public Object pictureCodeCheck(HttpServletRequest request,String pictureCode){
        String verifyCode = request.getSession().getAttribute("verifyCode").toString();

        //校验图片验证码
        if(!verifyCode.equals(pictureCode)) {
            throw new CrowdException("picture_code_error");
        }
        return Message.OK;

    }


    /**
     * 委托充值申请
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/applyRecharge")
    public Object applyRecharge(HttpServletRequest request,BigDecimal amount,String payPassword){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        Map response = null;// accountRechargeService.applyMobileRecharge(userId,amount.setScale(2,BigDecimal.ROUND_DOWN),payPassword);
//        Map map = new HashMap();
//        map.put("result","success");
//        map.put("data",response);
        return response;
    }


}
